﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.Common.Exceptions;

namespace Fuse8.DomainFramework.Common
{
    /// <summary>
    /// Utility for checking object characteristics
    /// </summary>
    public static class Guard
    {
        #region Methods

        /// <summary>
        /// Check that argument is not null
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        public static void ArgumentNotNull(object argument, string argumentName)
        {
            if (argument == null)
                throw new ArgumentNullException(
                    argumentName,
                    string.Format(CultureInfo.CurrentCulture, Resource.NullReferenceError, argumentName));
        }

        /// <summary>
        /// Check that collection is not empty
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="argumentName"></param>
        public static void ArgumentNotEmpty(ICollection collection, string argumentName)
        {
            ArgumentNotNull(collection, "collection");
            ArgumentNotEmpty(argumentName, "argumentName");

            if (collection.Count == 0)
            {
                throw new ArgumentOutOfRangeException(
                    string.Format(CultureInfo.CurrentCulture, Resource.CollectionCannotBeEmpty, argumentName));
            }
        }

        /// <summary>
        /// Check that collection is not empty
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="argumentName"></param>
        public static void ArgumentNotEmpty<T>(ICollection<T> collection, string argumentName)
        {
            ArgumentNotNull(collection, "collection");
            ArgumentNotEmpty(argumentName, "argumentName");

            if (collection.Count == 0)
            {
                throw new ArgumentOutOfRangeException(
                    string.Format(CultureInfo.CurrentCulture, Resource.CollectionCannotBeEmpty, argumentName));
            }
        }

        /// <summary>
        /// Check that string is not empty
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        public static void ArgumentNotEmpty(string argument, string argumentName)
        {
            if (string.IsNullOrEmpty(argumentName))
            {
                throw new ArgumentNullException(argumentName);
            }

            if (argument == null)
            {
                throw new ArgumentNullException(argumentName);
            }

            if (argument.Length == 0)
            {
                throw new ArgumentOutOfRangeException(
                    string.Format(CultureInfo.CurrentCulture, Resource.StringCannotBeEmpty, argumentName));
            }
        }

        #endregion
    }
}
